/**
 * www.jbarreto.com all rights reserved 2011
 */
package com.jbarreto.vc.actions;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jbarreto.vc.dataobjects.Pelicula;
import com.jbarreto.vc.forms.PeliculaForm;
import com.jbarreto.vc.plugin.HibernatePlugIn;

/**
 * @author JBarreto
 * 
 *         jj_b_f@yahoo.com.mx
 * 
 *         14/01/2011 10:51:58
 */
public class ModificarPeliculaAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		System.out.println("Getting session factory");
		/* Get the servlet context */
		ServletContext context = request.getSession().getServletContext();
		/* Retrieve Session Factory */
		SessionFactory _factory = (SessionFactory) context
				.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);
		/* Open Hibernate Session */
		Session session = _factory.openSession();
		int peliculaID = Integer.parseInt(request.getParameter("peliculaID"));
		System.out.println(peliculaID);
		PeliculaForm peliculaForm = (PeliculaForm) form;
		Pelicula pelicula = (Pelicula) session.get(Pelicula.class, peliculaID); // Criteria
		// Query
		// Example
		session.close();

		peliculaForm.setOperation("modifyPelicula");

		System.out.println("peliculaForm:" + peliculaForm);
		System.out.println("pelicula" + pelicula);

		//copyInfo(peliculaForm, pelicula);
		BeanUtils.copyProperties(peliculaForm, pelicula);
		// request.setAttribute("peliculaForm", peliculaForm);
		// form = peliculaForm;
		/* Close session */
		System.out.println("Hibernate Session Closed");

		return mapping.findForward("success");
	}

	private void copyInfo(PeliculaForm peliculaForm, Pelicula pelicula) {

		peliculaForm.setTitulo(pelicula.getTitulo());
		peliculaForm.setTituloOriginal(pelicula.getTituloOriginal());
		peliculaForm.setYearProduccion(pelicula.getYearProduccion());
		peliculaForm.setEstudio(pelicula.getEstudio());
		peliculaForm.setDuracion(pelicula.getDuracion());
		peliculaForm.setDirector(pelicula.getDirector());
		peliculaForm.setActores(pelicula.getActores());
		peliculaForm.setPaisOrigen(pelicula.getPaisOrigen());
		peliculaForm.setIdioma(pelicula.getIdioma());
		peliculaForm.setGenero(pelicula.getGenero());
		peliculaForm.setFechaEstreno(pelicula.getFechaEstreno());
		peliculaForm.setFormato(pelicula.getFormato());
		peliculaForm.setClasificacion(pelicula.getClasificacion());
		peliculaForm.setSeccion(pelicula.getSeccion());
		peliculaForm.setCostoRenta(pelicula.getCostoRenta());
		peliculaForm.setCosto(pelicula.getCosto());
	}

	public ActionForward createPelicula(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		boolean created = true;

		if (created) {
			return (mapping.findForward("create-success"));
		} else {
			return (mapping.findForward("create-failed"));
		}
	}

	public ActionForward modifyPelicula(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		boolean modified = true;

		if (modified) {
			return (mapping.findForward("modify-success"));
		} else {
			return (mapping.findForward("delete-failed"));
		}
	}

}
